CCCCC     MAIN  OF  FLEXIBLE  SPACECRAFT  DYNAMICS  PROGRAM     CCCCCCCC
CCCCC MAIN
C     THIS IS THE 'MAIN' FOR THE FLEXIBLE BODY SPACECRAFT DYNAMICS
C     COMPUTER PROGRAM WRITTEN BY AVCO SYSTEMS DIV. WILMINGTON,MASS.
C     THE FSD PROGRAM HAS BEEN MODIFIED  AND MAINTAINED
C     BY K. YONG  OF COMPUTER SCIENCES CORPORATION, SYSTEM
C     SCIENCES DIVISION FOR ADDITIONAL CAPABILITIES FOR
C     FLEXIBLE SPACECRAFT DYNAMIC SIMULATIONS
C
      IMPLICIT REAL*8 (A-H,O-Z)
      REAL*8 ISUBD,JARRAY,NSUBX
      INTEGER*4 ACNTRL,DDPLY,SDPLY
C
      DEFINE FILE 15(250,63,U,IDUM)
C
      COMMON/ADSTAT/ DER(150),DEP(150)
C
      COMMON/ANTENA/ A(10,3),ADOT(10,3),B(10,3),BDOT(10,3),DIN(10,3),
     .               DINDOT(10,3),DOUT(10,3),DOUTDT(10,3),ZBZ(3,10),
     .               NELMTS,NDAMPR,MODES(10)
C
      COMMON/CODPLY/ STANG,ANGTOL
C
      COMMON/CONSTS/ PI,TWOPI,RADIAN
C
      COMMON/CRATIO/ RATIO
C
      COMMON/CSOLAR/ SAO(10),SKA(9),SKB(9),SKOA(10,3),SKOB(10,3),
     .               STMK(10),SKAA(10,9),SKBB(10,9)
C
      COMMON/CSTVAL/ TSTART,ZL0(10),ZL1(10),ZL2(10),ZLA(10)
C
      COMMON/DATOUT/ IDATA,MLAST
C
      COMMON/DEBUG1/ IAFM(5)
C
      COMMON/DEBUG2/ IOUT,JOUT,KLUGE
C
      COMMON/DEBUG3/ ISWTCH
C
      COMMON/DEPLOY/ DDPLY,MDPLY
C
      COMMON/ECNSTS/ THETA1,THETGO(12)
C
      COMMON/EISUBK/ EI(10)
C
      COMMON/HAMOUT/ HAMILT,IHAMLT
C
      COMMON/ICNTRL/ KNTRL(10)
C
      COMMON/IMAIN1/ IDATE,LSAVE,INOPT,IPLOT,NUMEQS,IPLTPE,IORB,ITAPE
C
      COMMON/IODPLY/ ISDPLY,IRAXIS,ISAXIS,NCROSS,NPRINT
C
      COMMON/IPOOL1/ IGRAV,IDAMP,IK,K1,ITIM,IAB,IAPS,IBB,IBPS,NK(10),
     .               LK(10),LLK(10)
C
      COMMON/JBTEST/ IBTEST
C
      COMMON/LIBDPR/ ZK1D,ZK2D,PHIS,PHILD,DPHILD,BETLD,GAMLD,ZMDO,
     *               ZMDBO,CNV,DECAY
C
      COMMON/MOMENT/ ACNTRL,IVISCS,IATTDE,IMGMTS,IWHEEL,NPULSE
C
      COMMON/NODER / NDER,NOPT
C
      COMMON/OUTONE/ HSSA(6),SIMPX,SB(3)
C
      COMMON/PLOTIT/ XMIN(11),XMAX(11),YMIN(11),YMAX(11),IFSCAL,
     .               KPLOTS(450)
C
      COMMON/PRCOM / STORE(10,30),ILINE,ICOL,ICNT,IHD
C
      COMMON/PRESUR/ DTOO,POO
C
      COMMON/PUNCH / IPUN,IPUNCH
C
      COMMON/PWHEEL/ XMOMIN(3),DVMOM(3),VMOM(3)
C
      COMMON/RATTDE/ DTMXA,PXI,PXO,EIT,REFTIM,TMX1,TMX2,CMX,ISW
C
      COMMON/RMAIN1/ DELTAT,FACTOR,FREQ,TSTOP,DELMIT,
     .               UPBND(150),DNBND(150)
C
      COMMON/RMGNTC/ SMAGI(3),DPMAG(3),SFMAG,MAGFLD
C
      COMMON/RPOOL1/ RHOK(10),TIME,SA(3,3),FM1(3,3),ZLK(10),OMEG(3),
     .               ZLKP(10),ZLKDP(10),CMAT(3,3),GBAR(3,3),YBCM(3),
     .               ZBZK(3,10),FCM(3,3),DTO,PHID,PHI
C
      COMMON/RPOOL2/ PO,SD(3),DAN(3,10),DBN(3,10),CFMT(3,3),DIY1(3),
     .               SD1(3),DT1,P1,AERO,DTO1,YIZK(3),PO1
C
      COMMON/RPOOL3/ ZMS,YIZM(3,2)
C
      COMMON/RPOOL5/ CKMAT(3,3,10),FM2(3,3)
C
      COMMON/RPOOL8/ SZ01(10),SZ11(3,10),SZ24(9,10)
C
      COMMON/RSUNCL/ WE,TVER,ECLPTC
C
      COMMON/RTDIST/ TDIS(10)
C
      COMMON/RUNOUT/ LCPU
C
      COMMON/RVISCS/ NSUBX(3),YARRAY(3),RADTBE,VISCTY,RADRNG,DENSTY,
     .               JARRAY(3),SSUBY,OMEGL,ISUBD
C
      COMMON/SATPOS/ TLAST,TFRST,XLAST(3),XFRST(3),XDLAST(3),XDFRST(3),
     .               SDLAST(3),SDFRST(3),ADLAST(3),ADFRST(3),XDT
C
      COMMON/SUNANG/ DSL2
C
      COMMON/TJAN1/ T
C
      COMMON/VARBLS/ DEPEND(150),DERIV(150)
C
C
      COMMON/VERS  / VNO
C
      COMMON/XIN1  / PSI1,THET1,PHI1,ETTA,ZETTA,ITEST1
C
      COMMON/XIN2  / ALFAE,BETAE,GAMAE,OMBC(3),ITEST2
C
      COMMON/XIN3  / ALFAEK(10),BETAEK(10),GAMAEK(10),ETTAD,ZETTAD
C
      COMMON/XIN4  / UP(150),DN(150),CONSA,COMEG,AOOP,AOOPV,AOOP1,AOOPV1
     .              ,AIP,AIPV,AIP1,AIPV1,DOOP,DOOPV,DOOP1,DOOPV1,DIP
     .              ,DIPV,DIP1,DIPV1,DUC,DUC1,DUCD,DUCD1
C
      COMMON/ZSPINR/ DTZMA,PZDT,CMZO,ISPIN3,JSPIN
C
C            ADDITIONAL COMMON BLOCKS FOR MODIFICATION
C
      COMMON/ITCNTL/ IPULSE,ISPLSE,KPULSE,ITSW,IOTSW
C
      COMMON/HOUTPT/ IHCALC,IHREF,IHFLAG
C
      COMMON/IACC  / IACOMP,IHUBAC,ITIPAC,IAFLAG
C
      COMMON/GRNTST/ ALFAEG,DELTAG,PHASEG,ALTUDE,OMGY(3),
     *               GACC(3),GLOCAT(3),IGRUND,IALTUD ,IGASBR
C
      COMMON/ITW   / ITWIST,ITWST1
C
      COMMON/INTTRP/ ITPROT,NUMTIP(10),IRDBUG
C
      DIMENSION RWH(3)
C
      DATA INCOL1,IPASS/8888,0/
      DATA LDPLY,SDPLY/0,0/
C
      CALL ERRSET(73,.TRUE.,.TRUE.,.FALSE.,.TRUE.,15)
      CALL ERRSET(70,.TRUE.,.FALSE.,.FALSE.,.TRUE.)
C
      IPUN=0
      LAST=0
      SIMPX=0.0D0
      TLAST=40000.D0
C
      DO 10 I=1,150
      DERIV(I)=0.D0
      DEPEND(I)=0.D0
      DER(I)=0.D0
   10 DEP(I)=0.D0
      TWOPI=2.D0*PI
      RADIAN=PI/180.D0
C
   20 NOLD=N
      CALL READTH
      CALL READER
      IAMT=1
      CALL READIN (INCOL1,&1000)
      WRITE(6,2002)VNO
      IF(LDPLY.NE.0) CALL DSAVE(ZL1,ZLA,NELMTS,NDAMPR)
      CALL JETDM0(LDPLY)
      JOUT=IOUT
      TZSPIN=0.0D0
      CALL NUM
      IF (IDATA.EQ.0) NOLD=NUMEQS
      IDGT=NUMEQS-NOLD
      IF (IDGT .EQ. 0)  GO TO 11
      CALL RELOC2(DEP)
   11 CONTINUE
      CALL PRECHO(DEP)
      IF(IDATA.EQ.0) CALL ECHO
      IHD=1
      ILINE=0
      ICNT=90
      IPASS=1
      CALL HEDING(6)
      IF(IORB .NE. 0) CALL PDTAPR
      IF(IPLOT.GE.1) REWIND IPLTPE
      ICNT=60
      TSAVE=T
      CALL ACNVRT(IDATE,T)
      N=NUMEQS
C     CALL ADCAL(T,TVER,1)
      DSAVET=DELTAT
      FSAVE=FREQ
      IF(SDPLY.EQ.0) GO TO 25
      IF(MDPLY.NE.0) GO TO 30
      IF (LDPLY .EQ. 0)  GO TO 25
      GO TO 30
 25   CALL ADCAL(T,TVER,1)
C  25 CALL STVRBL(1,DEP,NSUBX,TSAVE)
      CALL STVRBL(1,DEP,NSUBX,TSAVE)
C                       TEST FOR CONTROL SYSTEMS
      IF(KNTRL(1).NE.0) CALL CSIC(1,T)
C
      CALL SBINIT
C
      CALL GPINIT
C
      CALL GMINIT
C
      CALL SAINIT
C
      CALL GMDINT
C
      CALL WHINIT
C
      CALL AWINIT
C
      CALL THINIT
C
      CALL VDINIT
C
      IF(MDPLY.NE.0) SDPLY=1
   30 IF(IDATA.NE.0) CALL ECHO
      IHD=1
      ILINE=0
      ICNT=90
      CALL SETVAL(1)
      CALL PCINIT(FRQ)
      CALL DCINIT(FRQ)
      CALL SACINT(FRQ)
      CALL BOUNDS
      IAFLAG=0
      IHFLAG=0
      IF(IACOMP.NE.0) IAFLAG=1
      IF(IHCALC.NE.0) IHFLAG=1
C
      CALL PRJACC(T,LDPLY)
C                       TEST FOR VISCOUS DAMPING
      IF(IVISCS .NE. 0) CALL VDIC
      TSTART=T
      TIME=T
      TTSTP=TSTOP+T
      TESTY=TTSTP-FRQ/100
      TZSPIN=TSTART + DTZMA
C
C
C
      TMX1=0.D0
      TMX2=0.D0
      LSAVE=1
      JUMP=1
      JSPN=1
      JSPN1=1
C
      IF(ISPIN3.EQ.0) GO TO 70
      CALL ZSPIN(TTSTP,FRQ,DELTAT,LSAVE,TSTART,JSPN)
C
   70 CALL ADMDUM(N,DELTAT,DEP,DER,UPBND,DNBND,FACTOR,FRQ,TTSTP,LSAVE,
     .            T,DELMIT)
C
      IAMT=2
      IF(LSAVE.EQ.6) GO TO 90
      IF(ISDPLY.EQ.1) CALL SUNDEP(2,SD)
      CALL BRANGE
      CALL PIDCNT(IPFLG)
      CALL DIDCNT(IPFLG)
      CALL SAPIDC(IPFLG)
      IF(IPFLG.EQ.0) GO TO 70
      IF(ITWIST.NE.0) CALL TWSTOT(DEP)
      IF(IHFLAG.NE.0) CALL HCOMPT
      IF(IAFLAG.NE.0) CALL ACCOMP
C
      IF(IGRUND.EQ.1) GO TO 73
      IF(INOPT.EQ.2) GO TO 71
      CALL SOUT(SA,OMEG,T,IPLOT,IPLTPE,PHI)
      IF(IAFM(3) .EQ. 0)  CALL SPRINT
      GO TO 72
   71 IF(IORB.EQ.0) CALL ADCAL(T,TVER,2)
      CALL GOUT(SA,OMEG,T,IPLOT,IPLTPE,PHI,TESTY)
      IF(IAFM(3) .EQ. 0)  CALL GPRINT
      GO TO 72
 73   CONTINUE
                       CALL TOUT(SA,OMEG,T,IPLOT,IPLTPE)
      IF(IAFM(3).EQ.0) CALL GPRINT
C
   72 IF(KLUGE.NE.0) GO TO 120
      IF(NPULSE.EQ.0) GO TO 80
      IF(IATTDE.NE.1.OR.(TMX1.EQ.0.D0.AND.TMX2.EQ.0.D0)) GO TO 80
      IF(ACNTRL.EQ.1)
     .  CALL ATTUDE(TTSTP,T,FRQ,DELTAT,NPULSE,LSAVE,JUMP)
C
   80 IF(ISPIN3.EQ.0) GO TO 85
      LSPIN=ISPIN(T,TZSPIN,TTSTP,DELTAT,JSPN1)
      IF(LSPIN.EQ.1) CALL ZSPIN(TTSTP,FRQ,DELTAT,LSAVE,TSTART,JSPN)
   85 IF(T.GE.TESTY) GO TO 110
      CALL REMTIM (LTIM,IJUNK)
      IF (LTIM.GT.LCPU) GO TO 70
      LAST=1
      WRITE (6,10002)
      IF(IAFM(2).EQ.1) IPUN=1
      GO TO 110
C
   90 IF(ACNTRL.EQ.0) GO TO 100
      WRITE(6,10003) TIME
      GO TO 110
  100 WRITE(6,10004) TIME
 110  CONTINUE
      IF(IAFM(5).EQ.1) CALL OUTFSD
      IF (IPLOT.EQ.0) GO TO 120
      END FILE IPLTPE
      REWIND IPLTPE
      CALL PLOTF
 120  CONTINUE
      IF((MLAST.EQ.0).AND.(MDPLY.EQ.0).AND.(DDPLY.EQ.0)) GO TO 138
      IF((MLAST.EQ.0).AND.(MDPLY.EQ.1).OR.(DDPLY.EQ.1)) GO TO 130
      IF(LAST.EQ.1) GO TO 140
      IF(IPUNCH.NE.0) CALL ECHO1
      CALL STVRBL(2,DEP,NSUBX,TSAVE)
      IF(KNTRL(1).NE.0) CALL CSIC(2,T)
      T=TSAVE
      DELTAT=DSAVET
      LDPLY=0
      MLAST=0
      MDPLY=0
      SDPLY=0
      KPULSE =0
      ITIM=1
      ISW=0
      DO 150 I=1,150
      DERIV(I)=0.0D0
      DEPEND(I)=0.0D0
      DER(I)=0.0D0
  150 DEP(I)=0.0D0
      GO TO 20
  130 DO 135 I=1,IK
      ZLA(I)=0.0D0
  135 ZL0(I)=ZLK(I)
  138 TIME=DMOD(TIME,8.64D4)
      T=TIME
      DELTAT=DSAVET
      FREQ=FSAVE
      LDPLY=LDPLY + 1
      SDPLY=1
      IF(IWHEEL.EQ.1) CALL WHEELS(2,RWH)
  140 IF(IPUN.EQ.1) CALL ECHO1
      IF(LAST.EQ.1) RETURN
      GO TO 20
1000  STOP
C
2002  FORMAT('0VERSION ',A8)
C
10002 FORMAT('1      ',//,' RUN BEING TERMINATED AS ALLOTED CPU TIME HAS
     1 BEEN USED UP ')
C
10003 FORMAT('0',10X,'DELAT-TIME TOO SMALL DURING VISCOUS DAMPING'/10X,
     .               'AT ',1PE15.8,' SECONDS')
C
10004 FORMAT('0',10X,'DELTA-TIME TOO SMALL'/10X,
     .               'AT ',1PE15.8,' SECONDS')
C
      END
